package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKLiquidation;
import controllers.BKSubsidiary;
import controllers.Cashbook;
import controllers.Logs;
import controllers.ScheduleCollections;

public class CashBook extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model dateModel = new Model();
	private Model particularsModel = new Model();
	private Model receivedModel = new Model();
	private Model disbursedModel = new Model();
	private Model balanceModel = new Model();
	
	
	
	public  CashBook () throws SQLException, IOException
	{
		TextField date = new TextField("date", dateModel);
		date.setRequired(true);
		
		TextField particulars = new TextField("particulars", particularsModel);
		particulars.setRequired(true);
		
		TextField received = new TextField("received", receivedModel);
		received.setRequired(true);
		
		TextField disbursed = new TextField("disbursed", disbursedModel);
		disbursed.setRequired(true);
		
		TextField balance = new TextField("balance", balanceModel);
		balance.setRequired(true);
		
		Form form = new Form("CashBookForm");
		
		form.add(new Button("add"){
		
		private static final long serialVersionUID = 1L;

		public void onSubmit(){
			
			try {
				String date = (String) dateModel.getObject();
				String particulars = (String) particularsModel.getObject();
				String received = (String) receivedModel.getObject();
				String disbursed = (String) disbursedModel.getObject();
				String balance = (String) balanceModel.getObject();
			
				String uploader = ((Sessions)getSession()).getDesc();
				
				Date today = new Date();
				Timestamp timestamp = new Timestamp(today.getTime());
				String ts = timestamp.toString();
				
				Cashbook c = new Cashbook();
				float nbalance = Float.valueOf(balance);
				c.setBalance(nbalance);
				c.setDate(date);
				c.setDateCreated(ts);
				float ndisbursed = Float.valueOf(disbursed);
				c.setDisbursed(ndisbursed);
				c.setParticulars(particulars);
				float nreceived = Float.valueOf(received);
				c.setReceived(nreceived);
				c.setUploader(uploader);
				sqlMap.insert("Cashbook.insert", c);
				
				Logs log = new Logs();
				log.setLog_activity("Cashbook: " + particulars);
				log.setLog_officer(uploader);
				log.setLog_time(ts);
				
				sqlMap.insert("Logs.insert", log);
			
				BookKeepersSubsidiaryList page = new BookKeepersSubsidiaryList();
				setResponsePage(page);
				error("added");
		}
			catch (SQLException e){
				 error("Fill up necessary fields");
				 e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}});
		
		add(new BookmarkablePageLink("manageForms", ManageForms.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", BookKeepersSubsidiaryList.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(date);
		form.add(particulars);
		form.add(received);
		form.add(disbursed);
		form.add(balance);
		
		add(form);
	}

}

